package com.demo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.dao.UserMapper;
import com.demo.entity.User;

@Service
public class UserService{
	@Autowired
	private UserMapper userMapper;

	public int deleteByPrimaryKey(Integer id) {
		return userMapper.deleteByPrimaryKey(id);
	}

	public int insert(User record) {
    	return userMapper.insert(record);
    }

	public User selectByPrimaryKey(Integer id) {
    	return userMapper.selectByPrimaryKey(id);
    }

	public List<User> selectAll(){
    	return userMapper.selectAll();
    }

	public int updateByPrimaryKey(User record) {
    	return userMapper.updateByPrimaryKey(record);
    }
	
	public User login(String name,String pwd,String roles) {
		User user = new User();
		if(userMapper.login(name, pwd, roles)!=null) {
			user = userMapper.login(name, pwd, roles);
		}else {
			user = new User();
		}
		return user;
	}
	
	public String findMaxId() {
		return userMapper.findMaxId();
	}

	public int register(String name,String pwd,String truename,String phone,String email,String sex,int age, String address) {
		int result = 0;
		User user = new User();
		String aString = userMapper.checkRegister(name);
		if(aString!=null) {
			int a = Integer.valueOf(aString);
			if(a>0) {
				result = 2;  
			}else {
				int id=0;
				if(userMapper.findMaxId()==null) {
					id = 1;
				}else {
					id = Integer.valueOf(userMapper.findMaxId())+1;
				}
				user.setId(id);
				user.setName(name);
				user.setRoles("Y");
				user.setPwd(pwd);
				user.setPhone(phone);
				user.setAddress(address);
				user.setTruename(truename);
				user.setSex(sex);
				user.setAge(age);
				user.setEmail(email);
				result = userMapper.insert(user);
			}
		}
		return result;
	}

}
